package com.yunhe.sms.service.system.impl;

import com.yunhe.commens.dto.Pagedao;
import com.yunhe.sms.dao.system.RoleDao;
import com.yunhe.sms.entity.system.Role;
import com.yunhe.sms.service.system.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LeiPeifeng
 * @version V1.0
 * @date 2017/10/24 0024 10:20
 * @Description: 实现角色RoleService 接口
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    /**
     * 把权限信息放到list中
     * 把角色id和角色的权限信息集合list放到map中，做批量插入用
     * @param rolId
     * @param premissionIds
     * @return
     */
    public Map<String,Object> packrolpremi(Long rolId ,String premissionIds){
        List<Long> list = new ArrayList<> ();
        Map<String,Object> map = new HashMap<> ();
        String[] split = premissionIds.split ( "," );
        for (String Premid : split){
            list.add ( new Long ( Premid ) );
        }
        map.put ( "rolId",rolId);
        map.put ( "premilist",list );
        return map;
    }

    @Override
    public void save(Role role,String premissionIds) {
        roleDao.save ( role );//当前用户的id自动存入实体中
        Map<String, Object> map = null;
        if (null != premissionIds && !premissionIds.isEmpty ()){
             map = packrolpremi ( role.getId (), premissionIds );
            roleDao.saveRolePremi ( map );//保存角色权限
        }

    }

    @Override
    public void deleted(String ids) {
        if (null != ids && !ids.isEmpty ()) {
            String[] split = ids.split ( "," );
            for (String id : split) {
                roleDao.deleted ( new Long(id) );//删除角色信息
                roleDao.deletedRolPremi ( new Long(id) );//删除角色相关的权信息
            }
        }
    }

    @Override
    public void update(Role role,String premssionIds) {
        roleDao.update ( role );//更新角色信息
        Long id = role.getId ();//获取角色的id
        roleDao.deletedRolPremi ( id );//删除原有的用户权限信息
        Map<String, Object> map = null;
        if (null != premssionIds && !premssionIds.isEmpty ()){
            map = packrolpremi ( role.getId (), premssionIds );
            roleDao.saveRolePremi ( map );//保存角色权限
        }
    }

    @Override
    public Role find(Long id) {
        return roleDao.find ( id );
    }

    @Override
    public List<Role> findList() {
        return roleDao.findList ();
    }

    @Override
    public List<Role> findListStatu() {
        return roleDao.findListStatu ();
    }

    @Override
    public Pagedao<Role> page(int offset, int limit) {
        Pagedao<Role> pagedao =new Pagedao<> ();
        List<Role> findpage=null;
        int count = roleDao.count ( );//总数
        if (count>0){
            findpage = roleDao.findpage ( offset, limit );//本业数据
            pagedao.setPage ( offset/limit+1 );
            pagedao.setSize ( limit );
        }
        pagedao.setTotal ( count);
        pagedao.setRows ( findpage );
        return pagedao;
    }
}
